import Message from 'uxcore-message';

import {AmapUserKey} from './../../app/variables';
import Camera from "../../components/camera";

const blueIcon =
  "<div class=\"hi-icon-wrap hi-icon-effect-8\">" +
  "    <span class=\"hi-icon-blue hi-icon-location\"/>" +
  "  </div>";

const blueAnimateIcon =
  "<div class=\"hi-icon-wrap hi-icon-effect-8\">" +
  "    <span class=\"hi-icon-blue-animate hi-icon-location\"/>" +
  "  </div>";

const greenIcon =
  "<div class=\"hi-icon-wrap hi-icon-effect-8\">" +
  "    <span class=\"hi-icon-green hi-icon-location\"/>" +
  "  </div>";

const greenAnimateIcon =
  "<div class=\"hi-icon-wrap hi-icon-effect-8\">" +
  "    <span class=\"hi-icon-green-animate hi-icon-location\"/>" +
  "  </div>";

const orangeIcon =
  "<div class=\"hi-icon-wrap hi-icon-effect-8\">" +
  "    <span class=\"hi-icon-orange hi-icon-location\"/>" +
  "  </div>";

const orangeAnimateIcon =
  "<div class=\"hi-icon-wrap hi-icon-effect-8\">" +
  "    <span class=\"hi-icon-orange-animate hi-icon-location\"/>" +
  "  </div>";

const redIcon =
  "<div class=\"hi-icon-wrap hi-icon-effect-8\">" +
  "    <span class=\"hi-icon-red hi-icon-location\"/>" +
  "  </div>";

const redAnimateIcon =
  "<div class=\"hi-icon-wrap hi-icon-effect-8\">" +
  "    <span class=\"hi-icon-red-animate hi-icon-location\"/>" +
  "  </div>";

const purpleIcon =
  "<div class=\"hi-icon-wrap hi-icon-effect-8\">" +
  "    <span class=\"hi-icon-purple hi-icon-location\"/>" +
  "  </div>";

const purpleAnimateIcon =
  "<div class=\"hi-icon-wrap hi-icon-effect-8\">" +
  "    <span class=\"hi-icon-purple-animate hi-icon-location\"/>" +
  "  </div>";

import StorageUtils from './../../app/storage'

const CANTON_COUNT_TAG = {
  A: '全国各省所在地人数',
  B: '宜昌外各市所在地人数',
  C: '宜昌各县市区所在地人数',
  D: '宜昌县市区目的地人数',
  E: '复工项目分布',
  F: '复工项目分布',
};

export default {
  defaults(props) {
    return {
      systemName: '',
      wHeight: window.innerHeight - 140,
      messageContentHeight: window.innerHeight - 777,
      // 标注类型：监督工程标注类型（在建ZJ，竣工JG）；扬尘预警标注类型（绿色green，黄色yellow，红色red）；消息提醒标注类型（红色red）
      markerCategory: {
        YC: ['all', 'green', 'yellow', 'red'], // 扬尘预警标注
        ENG: ['all', 'green', 'blue'], // 监督工程标注
        MSG: ['all', 'red'] // 提醒消息标注
      },

      isLogin: true,
      effect: 'slideDown',
      showPass: false,
      errorMsg: '',
      userconfig: {},
      ui_model: [],
      token: {},
      ant: {
        durationSeconds: 0.5,
        startAnimation: false,
        startStyle: {opacity: 0},
        endStyle: {opacity: 3}
      },

      // 提醒消息
      messageContent: {
        title: "消息通知",
        // 当前选择的消息
        message: {},
        // 全部消息列表
        messages: []
      },

      // 地图配置
      amapOptions: {
        amapPlugins: ['ToolBar'],
        amapEvents: {},

        city: '',
        center: {},
        zoom: 13,
        // 当前标注类型
        currentMarkerCategory: {
          type: 'ENG',
          color: 'all'
        },
        // 当前标注数据
        currentMarker: {
          opts: {
            position: {},
            content: '',
            offset: {
              x: -12,
              y: -12
            },
            animation: 'AMAP_ANIMATION_NONE',
            visible: true,
            title: null,
            draggable: false,
            topWhenClick: true,
            extData: {
              markerType: 'ENG', // 地图标注类型（'YC':扬尘；'ENG':监督工程）
              iconColor: '',
              markerId: null,
              showInfo: false,
              engInfo: {}
            }
          },
          customOpts: {
            markerType: 'ENG', // 地图标注类型（'YC':扬尘；'ENG':监督工程）
            id: null,
            name: '',
            addr: '',
            iconColor: '',
            markerId: '',
          }
        },
        currentMarkers: [],
        // 当前消息
        currentMsgMarker: {},
        currentMsgMarkers: [],
        // 地图标注弹出气泡
        infoWindow: {
          visible: false,
          position: {
            longitude: 0,
            latitude: 0
          },
          size: {
            width: 240,
            height: 180
          },
          offset: [0, 0]
        },
        infoWindowContent: {
          id: '',
          zhgdId: '',
          title: '',
          content: '',
          eData: [],
          camera: [],
          type: ''
        },
        heatmapProps: {
          visible: false,
          opacity: [0, 0.8],
          radius: 80,
          zooms: [3, 15],
          gradient: {
            '0.1': '#06C705',
            '0.2': '#FEFC02',
            '0.3': '#FF9931',
            '0.4': '#FB0004',
            '0.6': '#94339A',
            '1.0': '#003235'
          },
          dataSet: {
            data: [],
            max: 1000
          }
        },
        // 地图标点聚合配置
        markerClusterer: {
          opts: {
            gridSize: 50,
            minClusterSize: 4,
            maxZoom: 18,
            averageCenter: true,
            zoomOnClick: true
          }
        },
      },

      // 消息工程
      msgMarkers: [],

      // 扬尘工程
      ycMarkers: [],

      // 监督工程
      engMarkers: [],
      menuKey: 'default',

      // 消息历史
      xxlsOpts: {
        visible: false
      },

      amap: null,


      showfgMap: false,

      fgMapShow: false,

      cameraDialog: {
        title: '视频监控',
        visible: false,
        content: null
      },

      // 复工复产数据
      fgfc: {
        mapData: [],
        mapSelected: 'A',
      },

      // default页面劳务人员派工弹窗页面显示状态
      dispatchSumDialogVisible: false,
    }
  },

  // 人员分布图切换
  cantonCountMapChange(ctx, props) {
    getFgfcCantonCount(ctx, props);
  },

  // 显示隐藏弹窗
  showCameraDialog(ctx, props) {

    let content;
    if (props.visible) {
      content = <Camera data={{selectedCamera: props.selectedCamera}} token={ctx.getProps().token}/>;
    }

    ctx.setState(
      React.addons.update(ctx.getState(), {
        cameraDialog: {
          visible: {$set: props.visible},
          content: {$set: content}
        }
      })
    );
  },

  init(ctx, props) {
    if (StorageUtils.getToken()) {
      ctx.setState(React.addons.update(ctx.getState(), {
        token: {
          $set: StorageUtils.getToken()
        },
      }));

      refresh(ctx, {token: StorageUtils.getToken(), ...props});
    }
    else {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          ant: {
            startAnimation: {
              $set: false
            }
          },
          isLogin: {
            $set: false
          }
        }));
    }
  },

  login(ctx, props) {
    ctx.fn.DB.sys.auth(props.user).then((content) => {
        ctx.setState(
          React.addons.update(ctx.getState(), {

            isLogin: {
              $set: true
            },
            token: {
              $set: content.token
            }
          })
        );
        ctx.fn.DB.sys.resource({Authorization: content.token}).then((content) => {

          let amapEvents = {
            created: (map) => {
              createMap(ctx, {map: map, simpleUser: content.simpleUser});
            },
          };

          let ui_model = [];
          if (content && 'menuData' in content && content.menuData && content.menuData.length > 0) {
            content.menuData[0].children.map((menu) => {
              ui_model.push({data: menu.data, children: menu.children});
            });
          }
          ctx.setState(
            React.addons.update(ctx.getState(), {
              userconfig: {
                $set: content
              },
              ui_model: {$set: ui_model},
              ant: {
                startAnimation: {
                  $set: true
                }
              },
              amapOptions: {
                amapEvents: {$set: amapEvents},
                city: {$set: content.simpleUser.city}
              },
              systemName: {$set: content.simpleUser.name},
            })
          );
          // 是否有扬尘
          let hasYc = false;
          if (ui_model.length > 0) {
            ui_model.map((menu) => {
              if ('ycjk' === menu.data.url) {
                hasYc = true;
              }
            });
          }
          initCategory(ctx, props);
          getEngMarkers(ctx, props);
          if (hasYc) {
            getYcMarkers(ctx, props);
          }
          changeMenu(ctx, props);
        }, (error) => {
          console.log(error);
        });
      },
      (error) => {
        console.log(error);
        ctx.setState(
          React.addons.update(ctx.getState(), {
            errorMsg: {
              $set: error.message
            }
          })
        );
      }
    );
  },

  changeCurrentMarker(ctx, props, fromReturn) {
    if (fromReturn) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          amapOptions: {
            currentMarker: {$set: fromReturn}
          }
        })
      );
    }
  },

  changeCurrentMarkers2(ctx, props, fromReturn) {

    if (fromReturn && fromReturn.menuChanged) {
      if (props) {
        let markerType = 'ENG';
        if (props.key === 'ycjk') {
          markerType = 'YC';
        } else {
          markerType = 'ENG';
        }

        let currentMarkers = [];
        let currentMarker = {};
        if (markerType === 'ENG') {
          currentMarkers = ctx.getState().engMarkers;
        } else if (markerType === 'YC') {
          currentMarkers = ctx.getState().ycMarkers;
        }
        if (currentMarkers && currentMarkers.length > 0) {
          currentMarker = currentMarkers[0];
        }
        ctx.setState(
          React.addons.update(ctx.getState(), {
            amapOptions: {
              currentMarker: {
                $set: currentMarker
              },
              currentMarkers: {
                $set: currentMarkers
              },
              infoWindow: {
                visible: {$set: false}
              }
            }
          })
        );
      }
    }
  },

  setInfoWindowVisible(ctx, props) {
    if (props) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          amapOptions: {
            infoWindow: {
              visible: {$set: props.visible}
            }
          }
        })
      );
    }
  },

  // 标注图标类型初始化
  initCategory2(ctx, props) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        amapOptions: {
          currentMarkerCategory: {
            type: {$set: props.key === 'ycjk' ? 'YC' : 'ENG'},
            color: {$set: 'all'}
          }
        }
      })
    );
  },

  // 工程明细，页面选择工程后，显示项目明细信息
  onGcTitleClick2(ctx, props) {

    let updateAmapOptions = {};
    let clickedMarker = ctx.getState().engMarkers.find((marker) => {
      return marker.customOpts.id === props.id;
    });
    if (clickedMarker) {
      //
      updateAmapOptions = {
        center: [clickedMarker.opts.position.longitude, clickedMarker.opts.position.latitude],
        infoWindow: {
          visible: true,
          position: clickedMarker.opts.position
        },
        infoWindowContent: {
          id: clickedMarker.customOpts.id,
          zhgdId: clickedMarker.customOpts.zhgdId,
          title: clickedMarker.customOpts.name,
          content: clickedMarker.customOpts.addr,
          type: 'ENG',
          junGong: clickedMarker.customOpts.junGong
        }
      };

      ctx.setState(
        React.addons.update(ctx.getState(), {
          amapOptions: {
            center: {$set: updateAmapOptions.center},
            infoWindow: {$merge: updateAmapOptions.infoWindow},
            infoWindowContent: {$set: updateAmapOptions.infoWindowContent},
            currentMarkers: {
              $apply: (markers) => {
                return markers.map((marker) => {
                  if (marker.customOpts.id === props.id) {
                    let color = marker.customOpts.iconColor;
                    let iconContent = marker.opts.content;
                    switch (color) {
                      case 'green':
                        color = 'green2';
                        iconContent = greenAnimateIcon;
                        break;
                      case 'blue':
                        color = 'blue2';
                        iconContent = blueAnimateIcon;
                        break;
                      case 'red':
                        color = 'red2';
                        iconContent = redAnimateIcon;
                        break;
                      case 'yellow':
                        color = 'yellow2';
                        iconContent = orangeAnimateIcon;
                        break;
                    }
                    return React.addons.update(marker, {
                      customOpts: {
                        iconColor: {$set: color}
                      },
                      opts: {
                        content: {$set: iconContent}
                      }
                    })
                  } else {
                    let color = marker.customOpts.iconColor;
                    let iconContent = marker.opts.content;
                    switch (color) {
                      case 'green2':
                        color = 'green';
                        iconContent = greenIcon;
                        break;
                      case 'blue2':
                        color = 'blue';
                        iconContent = blueIcon;
                        break;
                      case 'red2':
                        color = 'red';
                        iconContent = redIcon;
                        break;
                      case 'yellow2':
                        color = 'yellow';
                        iconContent = orangeIcon;
                        break;
                    }
                    return React.addons.update(marker, {
                      customOpts: {
                        iconColor: {$set: color}
                      },
                      opts: {
                        content: {$set: iconContent}
                      }
                    });
                  }
                })
              }
            }
          }
        })
      );
      return {clickedMarker: clickedMarker};
    } else {
      // 在当前工程中没有查到
      console.log('没有该工程的标注信息！！！');
      ctx.setState(
        React.addons.update(ctx.getState(), {
          amapOptions: {
            infoWindow: {
              visible: {$set: false}
            },
            currentMarkers: {
              $apply: (markers) => {
                return markers.map((marker) => {
                  let color = marker.customOpts.iconColor;
                  let iconContent = marker.opts.content;
                  switch (color) {
                    case 'green2':
                      color = 'green';
                      iconContent = greenIcon;
                      break;
                    case 'blue2':
                      color = 'blue';
                      iconContent = blueIcon;
                      break;
                    case 'red2':
                      color = 'red';
                      iconContent = redIcon;
                      break;
                    case 'yellow2':
                      color = 'yellow';
                      iconContent = orangeIcon;
                      break;
                  }
                  return React.addons.update(marker, {
                    customOpts: {
                      iconColor: {$set: color}
                    },
                    opts: {
                      content: {$set: iconContent}
                    }
                  });
                })
              }
            }
          }
        })
      );
    }
  },

  // marker显示类型切换
  onCategoryClick(ctx, props) {

    let state = ctx.getState();
    let currentMarkerCategory = state.amapOptions.currentMarkerCategory;

    if (currentMarkerCategory.type === props.type) {
      if (currentMarkerCategory.color === props.color) {
        currentMarkerCategory.color = 'all';
      } else {
        currentMarkerCategory.color = props.color;
      }
    } else {
      currentMarkerCategory.type = props.type;
      currentMarkerCategory.color = props.color;
    }

    let currentMarkers;
    if (currentMarkerCategory.type === 'YC') {
      if (currentMarkerCategory.color === 'all') {
        currentMarkers = state.ycMarkers;
      } else {
        currentMarkers = state.ycMarkers.filter((marker) => {
          return marker.customOpts.iconColor === currentMarkerCategory.color
        });
      }
    } else {
      if (currentMarkerCategory.color === 'all') {
        currentMarkers = state.engMarkers;
      } else {
        currentMarkers = state.engMarkers.filter((marker) => {
          return marker.customOpts.iconColor === currentMarkerCategory.color
        });
      }
    }

    let currentMarker = {};
    let infoWindowContent = {
      id: '',
      zhgdId: '',
      title: '',
      content: '',
      type: 'ENG',
      junGong: false,
    };
    let infoWindow = state.amapOptions.infoWindow;
    if (currentMarkers && currentMarkers.length > 0) {
      currentMarker = currentMarkers[0];
      infoWindowContent = {
        id: currentMarker.customOpts.id,
        zhgdId: currentMarker.customOpts.zhgdId,
        title: currentMarker.customOpts.name,
        content: currentMarkerCategory.type === 'ENG' ? currentMarker.customOpts.addr : currentMarker.customOpts.pm10,
        type: currentMarkerCategory.type,
        junGong: currentMarker.customOpts.junGong,
      };
      infoWindow.visible = false;
      infoWindow.position = currentMarker.opts.position;
    }
    ctx.setState(
      React.addons.update(ctx.getState(), {
        amapOptions: {
          currentMarkerCategory: {
            $set: currentMarkerCategory
          },
          currentMarkers: {
            $set: currentMarkers
          },
          currentMarker: {
            $set: currentMarker
          },
          infoWindow: {
            $set: infoWindow
          },
          infoWindowContent: {
            $set: infoWindowContent
          }
        }
      })
    );
  },

  changeMenu2(ctx, props) {
    let menuChanged = ctx.getState().menuKey !== props.key;
    if (menuChanged) {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          menuKey: {$set: props.key},
          fgMapShow: {$set: false},
          amapOptions: {
            infoWindow: {
              visible: {$set: false}
            }
          }
        })
      );
    }

    initFgfcData(ctx, props);

    return {menuChanged: menuChanged};
  },

  // 编辑HeatMap Props
  setHeatMapVisible2(ctx, props) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        amapOptions: {
          heatmapProps: {
            visible: {
              $set: props.key === 'ycjk'
            }
          }
        }
      })
    )
  },

  // 扬尘预警消息页面提醒
  showYcAlertMessage(ctx, props) {
    const s = ctx.getState();
    if (s.menuKey === 'ycjk') {
      let update = {
        currentMarker: {},
        currentMsgMarker: {},
        menuKey: '',
        infoWindow: {},
        infoWindowContent: {},
        amapCenter: {}
      };
      update.infoWindow.size = {
        width: 400,
        height: 300
      };
      if (props.pcode === s.amapOptions.currentMarker.customOpts.id) {
        update.infoWindow.visible = !s.amapOptions.infoWindow.visible;
        update.currentMarker = s.amapOptions.currentMarker;
      } else {
        update.infoWindow.visible = true;
        update.currentMarker = s.amapOptions.currentMarkers.find(marker => marker.customOpts.id === props.glId);
      }

      update.infoWindow.position = update.currentMarker.opts.position;
      update.amapCenter = update.currentMarker.opts.position;
      update.infoWindowContent = {
        id: update.currentMarker.customOpts.id,
        zhgdId: update.currentMarker.customOpts.zhgdId,
        title: update.currentMarker.customOpts.name,
        content: '',
        type: 'YC',
        eData: update.currentMarker.customOpts.eData,
        // 喷淋雾炮状态
        sprinklestatus: update.currentMarker.customOpts.sprinklestatus,
        fogcannonstatus: update.currentMarker.customOpts.fogcannonstatus
      };

      if (update.infoWindow.visible) {
        getCamera(ctx, {engId: update.currentMarker.customOpts.id});
      }

      ctx.setState(
        React.addons.update(ctx.getState(), {
          amapOptions: {
            currentMarker: {$set: update.currentMarker},
            currentMarkers: {
              $apply: (markers) => {
                return markers.map(marker => {
                  let color = marker.customOpts.iconColor;
                  let iconContent = marker.opts.content;
                  if (marker.customOpts.id === props.glId) {
                    switch (color) {
                      case 'green':
                      case 'green2':
                        color = 'green2';
                        iconContent = greenAnimateIcon;
                        break;
                      case 'yellow':
                      case 'yellow2':
                        color = 'yellow2';
                        iconContent = orangeAnimateIcon;
                        break;
                      case 'red':
                      case 'red2':
                        color = 'red2';
                        iconContent = redAnimateIcon;
                        break;
                      default:
                        color = 'green';
                        iconContent = greenIcon;
                        break;
                    }
                    return React.addons.update(marker, {
                      customOpts: {
                        iconColor: {$set: color}
                      },
                      opts: {
                        content: {$set: iconContent}
                      }
                    });
                  } else {
                    return marker;
                  }
                })
              }
            },
            infoWindow: {$merge: update.infoWindow},
            infoWindowContent: {$merge: update.infoWindowContent},
            center: {$set: [update.amapCenter.longitude, update.amapCenter.latitude]}
          }
        })
      );

    } else {
      Message.info(`${props.glName}扬尘监测预警`, 4);
    }
  },

  // 地图标注点击后触发
  onMarkerClicked(ctx, props) {

    if (props) {

      let update = {
        currentMarker: {},
        currentMsgMarker: {},
        menuKey: '',
        infoWindow: {},
        infoWindowContent: {},
        amapCenter: {}
      };
      let s = ctx.getState();
      if (props.markerType === 'YC') {
        update.infoWindow.size = {
          width: 400,
          height: 300
        }
      } else {
        update.infoWindow.size = {
          width: 240,
          height: 180
        }
      }
      if (props.markerType === 'YC' || props.markerType === 'ENG') {

        let infoWindowContent = s.amapOptions.infoWindowContent;
        if (infoWindowContent.type === 'YC' || infoWindowContent.type === 'ENG') {
          if (props.markerId === s.amapOptions.currentMarker.customOpts.id) {
            update.infoWindow.visible = !s.amapOptions.infoWindow.visible;
          } else {
            update.infoWindow.visible = true;
          }
        } else {
          update.infoWindow.visible = true;
        }
        if (props.markerId === s.amapOptions.currentMarker.customOpts.id) {
          update.currentMarker = s.amapOptions.currentMarker;
        } else {
          update.currentMarker = s.amapOptions.currentMarkers.find((marker) => {
            return marker.customOpts.id === props.markerId
          });
        }

        update.infoWindow.position = update.currentMarker.opts.position;
        update.amapCenter = update.currentMarker.opts.position;
        if (props.markerType === 'ENG') {
          update.menuKey = 'xmmx';
          update.infoWindowContent = {
            id: update.currentMarker.customOpts.id,
            zhgdId: update.currentMarker.customOpts.zhgdId,
            title: update.currentMarker.customOpts.name,
            content: update.currentMarker.customOpts.addr,
            type: 'ENG',
            junGong: update.currentMarker.customOpts.junGong,
          };
        } else if (props.markerType === 'YC') {
          update.menuKey = 'ycjk';
          update.infoWindowContent = {
            id: update.currentMarker.customOpts.id,
            zhgdId: update.currentMarker.customOpts.zhgdId,
            title: update.currentMarker.customOpts.name,
            content: '',
            type: 'YC',
            // 喷淋雾炮状态
            sprinklestatus: update.currentMarker.customOpts.sprinklestatus,
            fogcannonstatus: update.currentMarker.customOpts.fogcannonstatus
          };

          if (update.infoWindow.visible) {
            getCamera(ctx, {engId: update.currentMarker.customOpts.id});
            getEData(ctx, {pcode: update.currentMarker.customOpts.id});
          }
        }

        ctx.setState(
          React.addons.update(ctx.getState(), {
            amapOptions: {
              currentMarker: {$set: update.currentMarker},
              currentMarkers: {
                $apply: (markers) => {
                  return markers.map((marker) => {
                    let color = marker.customOpts.iconColor;
                    let iconContent = marker.opts.content;
                    if (update.currentMarker.customOpts.id === marker.customOpts.id) {
                      switch (color) {
                        case 'green2':
                          color = 'green';
                          iconContent = greenIcon;
                          break;
                        case 'blue2':
                          color = 'blue';
                          iconContent = blueIcon;
                          break;
                        case 'red2':
                          color = 'red';
                          iconContent = redIcon;
                          break;
                        case 'yellow2':
                          color = 'yellow';
                          iconContent = orangeIcon;
                          break;
                        case 'green':
                          color = 'green2';
                          iconContent = greenAnimateIcon;
                          break;
                        case 'blue':
                          color = 'blue2';
                          iconContent = blueAnimateIcon;
                          break;
                        case 'red':
                          color = 'red2';
                          iconContent = redAnimateIcon;
                          break;
                        case 'yellow':
                          color = 'yellow2';
                          iconContent = orangeAnimateIcon;
                          break;
                        default:
                          break;
                      }
                    } else {
                      switch (color) {
                        case 'green2':
                        case 'green':
                          color = 'green';
                          iconContent = greenIcon;
                          break;
                        case 'blue2':
                        case 'blue':
                          color = 'blue';
                          iconContent = blueIcon;
                          break;
                        case 'red2':
                        case 'red':
                          color = 'red';
                          iconContent = redIcon;
                          break;
                        case 'yellow2':
                        case 'yellow':
                          color = 'yellow';
                          iconContent = orangeIcon;
                          break;
                        default:
                          break;
                      }
                    }
                    return React.addons.update(marker, {
                      customOpts: {
                        iconColor: {$set: color}
                      },
                      opts: {
                        content: {$set: iconContent}
                      }
                    });
                  })
                }
              },
              infoWindow: {$merge: update.infoWindow},
              infoWindowContent: {$set: update.infoWindowContent},
              center: {$set: [update.amapCenter.longitude, update.amapCenter.latitude]}
            },
            menuKey: {$set: update.menuKey}
          })
        );
      } else if (props.markerType === 'MSG') {
        update.currentMsgMarker = s.amapOptions.currentMsgMarkers.find((marker) => {
          return marker.customOpts.id === props.markerId
        });
        let infoWindowContent = s.amapOptions.infoWindowContent;
        if (infoWindowContent.type === props.markerType) {
          if (props.markerId === s.amapOptions.currentMsgMarker.customOpts.id) {
            update.infoWindow.visible = !s.amapOptions.infoWindow.visible;
          } else {
            update.infoWindow.position = update.currentMsgMarker.opts.position;
            update.infoWindow.visible = true;
          }
        } else {
          update.infoWindow.position = update.currentMsgMarker.opts.position;
          update.infoWindow.visible = true;
        }

        if (update.infoWindow.visible) {
          update.infoWindowContent = {
            id: update.currentMsgMarker.customOpts.id,
            zhgdId: update.currentMarker.customOpts.zhgdId,
            title: update.currentMsgMarker.customOpts.name,
            content: update.currentMsgMarker.customOpts.message,
            type: 'MSG',
          };
          update.amapCenter = update.currentMsgMarker.opts.position;

          ctx.setState(
            React.addons.update(ctx.getState(), {
              amapOptions: {
                currentMsgMarker: {$set: update.currentMsgMarker},
                infoWindow: {$merge: update.infoWindow},
                infoWindowContent: {$set: update.infoWindowContent},
                center: {$set: update.amapCenter}
              }
            })
          );
        } else {
          ctx.setState(
            React.addons.update(ctx.getState(), {
              amapOptions: {
                infoWindow: {
                  visible: {$set: false}
                },
              }
            })
          );
        }

      }
    }
  },

  // 扬尘实时数据更新
  updateYcsj(ctx, props) {

    const {
      ycData
    } = props;

    let iconContent;
    switch (ycData.alertColor) {
      case 'green':
        iconContent = greenIcon;
        break;
      case 'red':
        iconContent = redIcon;
        break;
      case 'yellow':
        iconContent = orangeIcon;
        break;
      default:
        iconContent = greenIcon;
        break;
    }

    ctx.setState(
      React.addons.update(ctx.getState(), {
        ycMarkers: {
          $apply: (ycMarkers) => {
            return ycMarkers.map((ycMarker) => {
              if (ycMarker.customOpts.id === ycData.id) {
                return React.addons.update(ycMarker, {
                  customOpts: {
                    pm10: {$set: ycData.pm10},
                    pm2: {$set: ycData.pm2},
                    iconColor: {$set: ycData.alertColor},
                    // 喷淋雾炮状态
                    sprinklestatus: {$set: ycData.sprinklestatus},
                    fogcannonstatus: {$set: ycData.fogcannonstatus},
                    eData: {
                      $set: {
                        ...ycData.md,
                        windDeriction: ycData.md.windDerictionDesc
                      }
                    }
                  },
                  opts: {
                    content: {$set: iconContent}
                  }
                })
              } else {
                return ycMarker;
              }
            })
          }
        },
        amapOptions: {
          currentMarkers: {
            $apply: (ycMarkers) => {
              return ycMarkers.map((ycMarker) => {
                if (ycMarker.customOpts.id === ycData.id) {
                  return React.addons.update(ycMarker, {
                    customOpts: {
                      pm10: {$set: ycData.pm10},
                      pm2: {$set: ycData.pm2},
                      iconColor: {$set: ycData.alertColor},
                      // 喷淋雾炮状态
                      sprinklestatus: {$set: ycData.sprinklestatus},
                      fogcannonstatus: {$set: ycData.fogcannonstatus},
                      eData: {
                        $set: {
                          ...ycData.md,
                          windDeriction: ycData.md.windDerictionDesc
                        }
                      }
                    },
                    opts: {
                      content: {$set: iconContent}
                    }
                  });
                } else {
                  return ycMarker;
                }
              })
            }
          },
          currentMarker: {
            $apply: (ycMarker) => {
              if (ycMarker.customOpts.id === ycData.id) {
                return React.addons.update(ycMarker, {
                  customOpts: {
                    pm10: {$set: ycData.pm10},
                    pm2: {$set: ycData.pm2},
                    iconColor: {$set: ycData.alertColor},
                    // 喷淋雾炮状态
                    sprinklestatus: {$set: ycData.sprinklestatus},
                    fogcannonstatus: {$set: ycData.fogcannonstatus},
                    eData: {
                      $set: {
                        ...ycData.md,
                        windDeriction: ycData.md.windDerictionDesc
                      }
                    }
                  },
                  opts: {
                    content: {$set: iconContent}
                  }
                })
              } else {
                return ycMarker;
              }
            }
          },
          heatmapProps: {
            dataSet: {
              data: {
                $apply: (data) => {
                  return data.map((item) => {
                    if (item.lng === ycData.lng && item.lat === ycData.lat) {
                      return React.addons.update(item, {
                        count: {$set: ycData.pm10},
                      });
                    } else {
                      return item;
                    }
                  })
                }
              }
            }
          }
        }
      })
    );
  },

  // 更新
  updateInfoWindowContent(ctx, props) {
    if (ctx.getState().amapOptions.infoWindow.visible) {
      let infoWindowContent = ctx.getState().amapOptions.infoWindowContent;
      if ('ycData' in props) {
        if (infoWindowContent.id === props.ycData.id && infoWindowContent.type === 'YC') {
          ctx.setState(
            React.addons.update(ctx.getState(), {
              amapOptions: {
                infoWindowContent: {
                  eData: {
                    $set: {
                      ...props.ycData.md,
                      windDeriction: props.ycData.md.windDerictionDesc
                    }
                  },
                  // 喷淋雾炮状态
                  sprinklestatus: {$set: props.ycData.sprinklestatus},
                  fogcannonstatus: {$set: props.ycData.fogcannonstatus}
                }
              }
            })
          );
        }
      }
    }
  },

  // 初始化热力图数据
  initHeatMapData2(ctx, props) {
    if (ctx.getState().menuKey === 'ycjk') {
      let heatMapData = [];
      let ycMarkers = ctx.getState().ycMarkers;
      if (ycMarkers) {
        ycMarkers.forEach((marker) => {
          let point = {
            'lng': marker.opts.position.longitude,
            'lat': marker.opts.position.latitude,
            'count': marker.customOpts.pm10
          };
          heatMapData.push(point);
        })
      }
      ctx.setState(
        React.addons.update(ctx.getState(), {
          amapOptions: {
            heatmapProps: {
              visible: {$set: props.key === 'ycjk'},
              dataSet: {
                data: {
                  $set: heatMapData
                }
              }
            }
          }
        })
      );
    }
  },

  onMapDragend(ctx, props) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        amapOptions: {
          center: {$set: [props.center.lng, props.center.lat]}
        }
      })
    );
  },

  onMapZoomChange(ctx, props) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        amapOptions: {
          zoom: {$set: props.zoom},
          center: {$set: [props.center.lng, props.center.lat]}
        }
      })
    );
  },

  // 处理扬尘预警消息
  handleYcAlertMessage(ctx, props) {
    let messages = ctx.getState().messageContent.messages;
    // 将消息计入消息队列
    messages = [props].concat(messages);
    ctx.setState(
      React.addons.update(ctx.getState(), {
        messageContent: {
          messages: {$set: messages},
          // message: {$set: props}
        }
      })
    );
  },

  // 监督业务消息接收
  receiveJdMessage(ctx, props) {
    let messages = ctx.getState().messageContent.messages;
    messages = [props].concat(messages);
    if (props.longitude && props.latitude) {
      let msgMarkers = ctx.getState().msgMarkers;
      let isNewMarker = true;
      let newMarker;
      if (msgMarkers && msgMarkers.length > 0) {
        for (let i = 0; i < msgMarkers.length; i++) {
          if (msgMarkers[i].opts.position.longitude === props.longitude && msgMarkers[i].opts.position.latitude === props.latitude) {
            // 同一坐标，需判断是否是同一工程
            if (msgMarkers[i].customOpts.id === props.glId) {
              // 若是同一工程，则将消息内容合并显示
              msgMarkers[i].customOpts.message = msgMarkers[i].customOpts.message + ';[' + props.jssj + ']:' + props.txms;
            } else {
              // 若不是同一工程，则消息分开显示
            }
            isNewMarker = false;
            newMarker = msgMarkers[i];
            break;
          }
        }
      }

      // 不是同一坐标，则创建新的坐标
      if (isNewMarker) {
        newMarker = {
          opts: {
            position: {
              longitude: props.longitude,
              latitude: props.latitude,
            },
            content: purpleAnimateIcon,
            offset: [-12, -12],
            animation: 'AMAP_ANIMATION_NONE',
            visible: true,
            title: props.glName,
            draggable: false,
            topWhenClick: true,
            extData: {
              markerType: 'MSG', // 地图标注类型（'YC':扬尘；'ENG':监督工程）
              iconColor: 'purple',
              markerId: props.glId,
              showInfo: false,
              engInfo: {}
            }
          },
          customOpts: {
            markerType: 'MSG', // 地图标注类型（'YC':扬尘；'ENG':监督工程；'MSG':消息）
            id: props.glId,
            name: props.glName,
            message: '[' + props.jssj + ']:' + props.txms,
            iconColor: 'purple',
            markerId: props.glId
          }
        };
        msgMarkers.push(newMarker);
      }

      ctx.setState(
        React.addons.update(ctx.getState(), {
          messageContent: {
            messages: {$set: messages},
            message: {$set: newMarker}
          },
          msgMarkers: {$set: msgMarkers},
          amapOptions: {
            currentMsgMarkers: {$set: msgMarkers},
            currentMsgMarker: {$set: newMarker},
            center: {$set: [props.longitude, props.latitude]},
            infoWindow: {
              visible: {$set: true},
              position: {
                longitude: {$set: props.longitude},
                latitude: {$set: props.latitude}
              },
              size: {
                width: {$set: 300},
                height: {$set: 240}
              }
            },
            infoWindowContent: {
              id: {$set: props.glId},
              title: {$set: props.glName},
              content: {$set: newMarker.customOpts.message},
              type: {$set: 'MSG'}
            }
          }
        })
      );

    } else {
      ctx.setState(
        React.addons.update(ctx.getState(), {
          messageContent: {
            messages: {$set: messages},
            message: {$set: props}
          },
          amapOptions: {
            infoWindow: {
              visible: {$set: false}
            }
          }
        })
      );
    }
  },

  // 点击消息记录
  onMessageClick(ctx, props) {
    const {
      message
    } = props;
    const state = ctx.getState();
    let {
      amapOptions: {
        currentMsgMarker,
        currentMsgMarkers,
        infoWindow,
        infoWindowContent,
        currentMarker,
        currentMarkers,
      }
    } = state;

    if (message.type === 'JD') {
      infoWindow.size = {
        width: 300,
        height: 240
      };
      if (currentMsgMarker.opts && currentMsgMarker.opts.position.latitude === message.position.latitude
        && currentMsgMarker.opts.position.longitude === message.position.longitude) {
        infoWindow.visible = !infoWindow.visible;
        currentMsgMarker = {};
      } else {
        infoWindow.visible = true;
        infoWindow.position = message.position;
        let tempMarkers = currentMsgMarkers.filter(
          marker => marker.opts.position.longitude === message.position.longitude
            && marker.opts.position.latitude === message.position.latitude
        );
        infoWindowContent.id = message.glId;
        infoWindowContent.title = message.glName;
        infoWindowContent.type = 'MSG';
        infoWindowContent.content = tempMarkers[0].customOpts.message;
        currentMsgMarker = tempMarkers[0];
      }

      ctx.setState(
        React.addons.update(ctx.getState(), {
          amapOptions: {
            currentMsgMarker: {$set: currentMsgMarker},
            center: {$set: [message.position.longitude, message.position.latitude]},
            infoWindow: {$set: infoWindow},
            infoWindowContent: {$set: infoWindowContent}
          }
        })
      );
    } else if (message.type === 'YA' || message.type === 'RA') {
      let update = {
        currentMarker: {},
        currentMsgMarker: {},
        menuKey: '',
        infoWindow: {},
        infoWindowContent: {},
        amapCenter: {}
      };
      update.infoWindow.size = {
        width: 400,
        height: 300
      };
      if (props.pcode === currentMarker.customOpts.id) {
        update.infoWindow.visible = infoWindow.visible;
        update.currentMarker = currentMarker;
      } else {
        update.infoWindow.visible = true;
        update.currentMarker = currentMarkers.find(marker => marker.customOpts.id === message.glId);
      }

      update.infoWindow.position = update.currentMarker.opts.position;
      update.amapCenter = update.currentMarker.opts.position;
      update.infoWindowContent = {
        id: update.currentMarker.customOpts.id,
        title: update.currentMarker.customOpts.name,
        content: '',
        type: 'YC',
        eData: update.currentMarker.customOpts.eData,
        // 喷淋雾炮状态
        sprinklestatus: update.currentMarker.customOpts.sprinklestatus,
        fogcannonstatus: update.currentMarker.customOpts.fogcannonstatus
      };

      if (update.infoWindow.visible) {
        getCamera(ctx, {engId: update.currentMarker.customOpts.id});
      }

      ctx.setState(
        React.addons.update(ctx.getState(), {
          amapOptions: {
            currentMarker: {$set: update.currentMarker},
            currentMarkers: {
              $apply: (markers) => {
                return markers.map(marker => {
                  let color = marker.customOpts.iconColor;
                  let iconContent = marker.opts.content;
                  if (marker.customOpts.id === message.glId) {
                    switch (color) {
                      case 'green':
                      case 'green2':
                        color = 'green2';
                        iconContent = greenAnimateIcon;
                        break;
                      case 'yellow':
                      case 'yellow2':
                        color = 'yellow2';
                        iconContent = orangeAnimateIcon;
                        break;
                      case 'red':
                      case 'red2':
                        color = 'red2';
                        iconContent = redAnimateIcon;
                        break;
                      default:
                        color = 'green';
                        iconContent = greenIcon;
                        break;
                    }
                    return React.addons.update(marker, {
                      customOpts: {
                        iconColor: {$set: color}
                      },
                      opts: {
                        content: {$set: iconContent}
                      }
                    });
                  } else {
                    return marker;
                  }
                })
              }
            },
            infoWindow: {$merge: update.infoWindow},
            infoWindowContent: {$merge: update.infoWindowContent},
            center: {$set: [update.amapCenter.longitude, update.amapCenter.latitude]}
          }
        })
      );

    }

  },

  // 消息历史页面显示隐藏
  setXxlsVisible2(ctx, props) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        xxlsOpts: {
          visible: {$set: props.xxlsVisible}
        }
      })
    );
  }

}

export function initCategory(ctx, props) {
  ctx.setState(
    React.addons.update(ctx.getState(), {
      amapOptions: {
        currentMarkerCategory: {
          type: {$set: props.key === 'ycjk' ? 'YC' : 'ENG'},
          color: {$set: 'all'}
        }
      }
    })
  );
}

export function changeMenu(ctx, props) {
  if (ctx.getState().menuKey !== props.key) {
    ctx.setState(
      React.addons.update(ctx.getState(), {
        menuKey: {$set: props.key},
        fgMapShow: {$set: false},
        amapOptions: {
          infoWindow: {
            visible: {$set: false}
          }
        }
      })
    );
  }

  initFgfcData(ctx, props);
}

export async function getEngMarkers(ctx, props) {

  const engContent = await ctx.fn.DB.amap.markers({Authorization: ctx.getState().token}).catch(
    (error) => {
      console.log(error);
    }
  );
  if (engContent) {
    let markers = [];
    engContent.forEach((eng) => {
      if (eng.lng && eng.lat) {
        let iconColor = 'green';
        if (eng.junGong) {
          iconColor = 'blue';
        } else {
          iconColor = 'green';
        }
        let iconContent = greenIcon;
        switch (iconColor) {
          case 'red':
            iconContent = redIcon;
            break;
          case 'red2':
            iconContent = redAnimateIcon;
            break;
          case 'green':
            iconContent = greenIcon;
            break;
          case 'green2':
            iconContent = greenAnimateIcon;
            break;
          case 'blue':
            iconContent = blueIcon;
            break;
          case 'blue2':
            iconContent = blueAnimateIcon;
            break;
          default:
            iconContent = greenIcon;
            break;
        }
        let zhgdId = eng.id;
        if (eng.id === '8a9b954c6dd2d7cb016dd898c6fd0639') {
          zhgdId = '8a9b954762bc71c10163395b02d00b12';
        } else if (eng.id === '8a9b954c6dd2d7cb016dd8927dbc061b') {
          zhgdId = 'b7580531-6091-439b-8853-43df0374414a';
        } else if (eng.id === '8a9b954c6dd2d7cb016dd890d9050604') {
          zhgdId = 'c07296bd-9a37-43ab-949a-d9f33a1edff3';
        } else if (eng.id === '8a9b954c6dd2d7cb016dd88ef9d505ed') {
          zhgdId = 'e3f01209-4631-4baf-ae2d-404de78874d9';
        }
        let marker = {
          opts: {
            position: {
              longitude: eng.lng,
              latitude: eng.lat,
            },
            content: iconContent,
            offset: {
              x: -12,
              y: -12
            },
            animation: 'AMAP_ANIMATION_NONE',
            visible: true,
            title: eng.name,
            draggable: false,
            topWhenClick: true,
            extData: {
              markerType: 'ENG', // 地图标注类型（'YC':扬尘；'ENG':监督工程）
              iconColor: iconColor,
              markerId: eng.id,
              showInfo: false,
              engInfo: {}
            }
          },
          customOpts: {
            markerType: 'ENG', // 地图标注类型（'YC':扬尘；'ENG':监督工程）
            id: eng.id,
            name: eng.name,
            addr: eng.addr,
            iconColor: iconColor,
            markerId: eng.id,
            zhgdId,
            junGong: eng.junGong,
          }
        };
        markers.push(marker);
      }
    });

    ctx.setState(
      React.addons.update(ctx.getState(), {
        engMarkers: {
          $set: markers
        }
      })
    );

    if (props.key !== 'ycjk') {
      initCurrentMarker(ctx, props);
    }
  }
}

export async function getEData(ctx, props) {
  const content = await ctx.fn.DB.ycjk.edata({Authorization: ctx.getProps().token, ...props}).catch(
    (error) => {
      console.log(error);
    }
  );

  if (content && content.length > 0) {

    const engId = content[0].id;
    let zhgdId;
    if (engId === '8a9b954c6dd2d7cb016dd898c6fd0639') {
      zhgdId = '8a9b954762bc71c10163395b02d00b12';
    } else if (engId === '8a9b954c6dd2d7cb016dd8927dbc061b') {
      zhgdId = 'b7580531-6091-439b-8853-43df0374414a';
    } else if (engId === '8a9b954c6dd2d7cb016dd890d9050604') {
      zhgdId = 'c07296bd-9a37-43ab-949a-d9f33a1edff3';
    } else if (engId === '8a9b954c6dd2d7cb016dd88ef9d505ed') {
      zhgdId = 'e3f01209-4631-4baf-ae2d-404de78874d9';
    } else {
      zhgdId = engId;
    }
    ctx.setState(
      React.addons.update(ctx.getState(), {
        amapOptions: {
          infoWindowContent: {
            eData: {
              $set: {
                ...content[0],
                zhgdId,
              }
            }
          }
        }
      })
    );
  }

}

export async function getCamera(ctx, props) {
  const content = await ctx.fn.DB.ycjk.queryCamera({Authorization: ctx.getState().token, ...props}).catch(
    (error) => {
      console.log(error);
    }
  );

  ctx.setState(
    React.addons.update(ctx.getState(), {
      amapOptions: {
        infoWindowContent: {
          camera: {$set: content}
        }
      }
    })
  );
}

export async function getYcMarkers(ctx, props) {

  const content = await ctx.fn.DB.amap.yc.markers({Authorization: ctx.getState().token}).catch(
    (error) => {
      console.log(error);
    }
  );

  if (content) {

    let ycMarkers = [];
    content.map((marker) => {

      let iconColor = marker.alertColor;
      let iconContent = greenIcon;
      switch (iconColor) {
        case 'green':
          iconContent = greenIcon;
          break;
        case 'green2':
          iconContent = greenAnimateIcon;
          break;
        case 'yellow':
          iconContent = orangeIcon;
          break;
        case 'yellow2':
          iconContent = orangeAnimateIcon;
          break;
        case 'red':
          iconContent = redIcon;
          break;
        case 'red2':
          iconContent = redAnimateIcon;
          break;
        default:
          iconContent = greenIcon;
          break;
      }
      let ycMarker = {
        opts: {
          position: {
            longitude: marker.lng,
            latitude: marker.lat,
          },
          content: iconContent,
          offset: {
            x: -12,
            y: -12
          },
          animation: 'AMAP_ANIMATION_NONE',
          visible: true,
          title: marker.name,
          draggable: false,
          topWhenClick: true,
          extData: {
            markerType: 'YC', // 地图标注类型（'YC':扬尘；'ENG':监督工程）
            iconColor: iconColor,
            markerId: marker.id,
            showInfo: false,
            engInfo: {}
          }
        },
        customOpts: {
          markerType: 'YC', // 地图标注类型（'YC':扬尘；'ENG':监督工程）
          id: marker.id,
          name: marker.name,
          addr: marker.addr,
          iconColor: iconColor,
          markerId: marker.id,
          pm10: marker.pm10,
          pm2: marker.pm2
        }
      };

      ycMarkers.push(ycMarker);

    });

    ctx.setState(
      React.addons.update(ctx.getState(), {
        ycMarkers: {
          $set: ycMarkers
        }
      })
    );

    if (props.key === 'ycjk') {
      initHeatMapData(ctx, props);
      initCurrentMarker(ctx, props);
    }
  }
}

export function initCurrentMarker(ctx, props) {

  if (props) {
    let markerType = props.key === 'ycjk' ? 'YC' : 'ENG';

    let result = {
      currentMarkers: [],
      currentMarker: {},
      infoWindowContent: {
        id: '',
        title: '',
        content: markerType ? {} : '',
        type: markerType
      },
      infoWindow: {
        visible: false,
        position: {
          longitude: 0,
          latitude: 0
        }
      }
    };

    result.currentMarkers = props.key === 'ycjk' ? ctx.getState().ycMarkers : ctx.getState().engMarkers;

    if (result.currentMarkers && result.currentMarkers.length > 0) {
      result.currentMarker = result.currentMarkers[0];
      result.infoWindowContent.id = result.currentMarker.customOpts.id;
      result.infoWindowContent.zhgdId = result.currentMarker.customOpts.zhgdId;
      result.infoWindowContent.title = result.currentMarker.customOpts.name;
      result.infoWindowContent.content = result.currentMarker.customOpts.addr;
      result.infoWindowContent.junGong = result.currentMarker.customOpts.junGong;
      result.infoWindowContent.type = markerType;
      result.infoWindow.position = result.currentMarker.opts.position;
    }
    ctx.setState(
      React.addons.update(ctx.getState(), {
        amapOptions: {
          currentMarker: {
            $set: result.currentMarker
          },
          currentMarkers: {
            $set: result.currentMarkers
          },
          infoWindowContent: {
            $set: result.infoWindowContent
          },
          infoWindow: {
            $merge: result.infoWindow
          }
        }
      })
    );
  }
}

// 初始化热力图数据
export function initHeatMapData(ctx, props) {
  let heatMapData = [];
  let ycMarkers = ctx.getState().ycMarkers;
  if (ycMarkers) {
    ycMarkers.forEach((marker) => {
      let point = {
        'lng': marker.opts.position.longitude,
        'lat': marker.opts.position.latitude,
        'count': marker.customOpts.pm10
      };
      heatMapData.push(point);
    })
  }
  ctx.setState(
    React.addons.update(ctx.getState(), {
      amapOptions: {
        heatmapProps: {
          visible: {$set: props.key === 'ycjk'},
          dataSet: {
            data: {
              $set: heatMapData
            }
          }
        }
      }
    })
  );
}

export function refresh(ctx, props) {
  ctx.fn.DB.sys.refresh({Authorization: props.token}).then((content) => {
      ctx.setState(React.addons.update(ctx.getState(), {
        token: {
          $set: content.token
        },
        isLogin: {
          $set: true
        }
      }));

      ctx.fn.DB.sys.resource({Authorization: content.token}).then((content) => {

        let amapEvents = {
          created: (map) => {
            createMap(ctx, {map: map, simpleUser: content.simpleUser});
          },
        };
        ctx.fn.DB.geocode.geo({
          key: AmapUserKey,
          address: content.simpleUser.city,
          city: content.simpleUser.city
        }).then((location) => {

          let center = location.split(',');
          let ui_model = [];
          if (content && 'menuData' in content && content.menuData && content.menuData.length > 0) {
            content.menuData[0].children.map((menu) => {
              ui_model.push({data: menu.data, children: menu.children});
            });
          }
          ctx.setState(
            React.addons.update(ctx.getState(), {
              userconfig: {
                $set: content
              },
              ui_model: {$set: ui_model},
              ant: {
                startAnimation: {
                  $set: true
                }
              },
              amapOptions: {
                amapEvents: {$set: amapEvents},
                city: {$set: content.simpleUser.city},
                center: {$set: {longitude: center[0], latitude: center[1]}},
                zoom: {$set: 13},
              },
              systemName: {$set: content.simpleUser.name}
            })
          );

          // 是否有扬尘
          let hasYc = false;
          if (ui_model.length > 0) {
            ui_model.map((menu) => {
              if ('ycjk' === menu.data.url) {
                hasYc = true;
              }
            });
          }
          initCategory(ctx, props);
          getEngMarkers(ctx, props);
          if (hasYc) {
            getYcMarkers(ctx, props);
          }
          changeMenu(ctx, props);
        });

      }, (error) => {
        console.log(error);
      });
    },
    (error) => {
      console.log(error);
    });
}

export function createMap(ctx, props) {
  let map = props.map;
  map.setCity(props.simpleUser.city, (e) => {
    map.setZoom(13);
    ctx.setState(
      React.addons.update(ctx.getState(), {
        amapOptions: {
          center: {$set: {longitude: e[0], latitude: e[1]}}
        },
        amap: {$set: map}
      })
    );
  });

  map.on('dragend', () => {
    let param = {
      center: map.getCenter()
    };
    onMapDragend(ctx, param);
  });
  map.on('zoomend', () => {
    let param = {
      center: map.getCenter(),
      zoom: map.getZoom()
    };
    onMapZoomChange(ctx, param);
  });
}

export function onMapDragend(ctx, props) {
  ctx.setState(
    React.addons.update(ctx.getState(), {
      amapOptions: {
        center: {$set: [props.center.lng, props.center.lat]}
      }
    })
  );
}

export function onMapZoomChange(ctx, props) {
  ctx.setState(
    React.addons.update(ctx.getState(), {
      amapOptions: {
        zoom: {$set: props.zoom},
        center: {$set: [props.center.lng, props.center.lat]}
      }
    })
  );
}

export async function initFgfcData(ctx, props) {

  if (props.key === 'fgfc') {
    getFgfcCantonCount(ctx, {mapSelected: 'A'});
  }

}

// 获取复工人员分布数据
export async function getFgfcCantonCount(ctx, props) {
  const {mapSelected = 'A'} = props;
  if (mapSelected === 'E' || mapSelected === 'F') {

    // 查询县市区项目复工审核各阶段的数量
    const stageCount = await ctx.fn.DB.fgfc.stage.count({Authorization: ctx.getState().token}).catch(
      error => {
        console.log(error);
      }
    );
    const districtMap = {
      市区: ['西陵区', '高新区', '伍家岗区', '点军区'],
      猇亭区: ['猇亭区'],
      夷陵区: ['夷陵区'],
      远安县: ['远安县'],
      兴山县: ['兴山县'],
      秭归县: ['秭归县'],
      长阳土家族自治县: ['长阳土家族自治县'],
      五峰土家族自治县: ['五峰土家族自治县'],
      宜都市: ['宜都市'],
      当阳市: ['当阳市'],
      枝江市: ['枝江市'],
    };

    let mapData = [];
    if (mapSelected === 'E') {
      mapData = stageCount.filter(item => item.shzt === '办理完毕');
    } else if (mapSelected === 'F') {
      mapData = stageCount;
    }
    ctx.setState(
      React.addons.update(ctx.getState(), {
        fgfc: {
          mapData: {$set: mapData},
          mapSelected: {$set: mapSelected},
        }
      })
    );
  } else {
    const cantonCount = await ctx.fn.DB.fgfc.canton.count({
      Authorization: ctx.getState().token,
      tag: CANTON_COUNT_TAG[mapSelected]
    }).catch(
      (error) => {
        console.log(error);
      }
    );
    ctx.setState(
      React.addons.update(ctx.getState(), {
        fgfc: {
          mapData: {$set: cantonCount},
          mapSelected: {$set: mapSelected},
        }
      })
    );
  }
}
